home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LDB171.ARJ / EXAMP502.CPP < prev    next >
C/C++ Source or Header  |  1992-05-12  |  2KB  |  67 lines

  1.      // examp502.cpp - link with binder.obj
  2.      // rework of examp402.cpp
  3.  
  4.      #define sfile "examp502.txt"
  5.  
  6.      #include <string.h>
  7.      #include <iostream.h>
  8.      #include "binder.hpp"
  9.  
  10.      struct str {
  11.        char *s;
  12.        str(const char *cs = (char *)0)  // default constructor
  13.       { s = (cs? strdup(cs) : (char *)0); }
  14.        str(str& si)                     // copy initialize
  15.         { s = (si.s? strdup(si.s) : si.s); }
  16.        void operator=(str& si)          // assignment
  17.         { delete s; s = (si.s? strdup(si.s) : si.s); }
  18.        ~str() { delete s; }             // destructor
  19.      };
  20.  
  21.      ostream& operator<<(ostream& os, str& si)
  22.      {
  23.       int i = (si.s? strlen(si.s) : 0);
  24.  
  25.       os << i << BDRendm;
  26.       if (i)
  27.            os.write(si.s,i);
  28.       return os;
  29.      }
  30.      istream& operator>>(istream& is, str& si)
  31.      {
  32.       char * D;
  33.       int i;
  34.  
  35.       is >> i >> BDRnextm;
  36.       if ((D = new char[i+i]) != (char *)0)  {
  37.            if (i)
  38.             is.read(D,i);
  39.            D[i] = '\0';
  40.            si.s = D;
  41.       }
  42.       return is;
  43.      }
  44.  
  45.      #define  FType    str
  46.      #define  FBinder  StrBdr
  47.      #define  FBindeR  StrBdR
  48.  
  49.      #include "fbinder.hpp"
  50.  
  51.      main()
  52.      {
  53.        StrBdr sb(BDR_DDELETE | BDR_DNEW | BDR_DSTORE);
  54.  
  55.        sb.ins(new str("line one"));
  56.        str s("line two");
  57.        sb.insNew(&s);
  58.        sb.save(sfile);
  59.  
  60.        StrBdR sB = new StrBdr(sfile);
  61.        if (!sB)  return 1;
  62.        sB->setCurNode();      // reset current node
  63.        while (sB->next())  cout << ((str *)*sB)->s << "\n";
  64.        delete sB;
  65.        return 0;
  66.      }
  67.